package org.jeecg.modules.middleware.rabbitmq.gas;


import com.alibaba.fastjson.JSONObject;
import com.rabbitmq.client.Channel;
import org.jeecg.modules.third.nucarf.GasService;
import org.springframework.amqp.core.ExchangeTypes;
import org.springframework.amqp.core.Message;
import org.springframework.amqp.rabbit.annotation.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@RabbitListener(bindings = @QueueBinding(
        value = @Queue(value = "gasQueue" ,durable = "true"),
        exchange = @Exchange(value = "gasExchange", durable = "true",type = ExchangeTypes.DIRECT),
        key = "gas"
))
public class GasRabbitMqListener {

    @Autowired
    private GasService gasService;

    @RabbitHandler
    public void onMessage(String msg , Message message, Channel channel) throws Exception {
        try {
            JSONObject jo = JSONObject.parseObject(msg);
            String c1 = jo.getString("channel");
            //充值
            if("1".equals(c1)) {
                String id = jo.getString("msg");
                gasService.handleRecharge(id);
                return;
            }
            //回收
            if("2".equals(c1)){
                String id = jo.getString("msg");
                gasService.handleBack(id);
                return;
            }
        }
        catch (Exception e) {
            e.printStackTrace();
        }finally {
            //消费消息
            channel.basicAck(message.getMessageProperties().getDeliveryTag(), false);
        }
    }
}
